<template>
    <div class="common-layout">
      <el-container>
        <el-header class="header">学习自习室</el-header>
        <el-container>
          <el-aside width="200px" class="aside">功能菜单</el-aside>
          <el-main>
            <div v-if="studyPlan" class="study-session">
              <div class="left-panel">
                <h1 class="title">学习状态</h1>
                <div class="units">
                  <div class="panel">
                    <h1 class="bg-unit">C</h1>
                    <h2>距离课间休息时间还有：{{ formatTime(studyTime) }}</h2>
                  </div>
                  <div class="panel">
                    <h1 class="bg-unit">F</h1>
                    <h2>目标学习时间：{{ formatTime(studyDuration) }}</h2>
                  </div>
                  <div class="panel">
                    <h1 class="bg-unit">K</h1>
                    <h2>休息时间：{{ formatTime(restDuration) }}</h2>
                  </div>
                  <div class="panel goal-panel">
                    <h1 class="bg-unit">G</h1>
                    <h2>当前学习目标：{{ goal }}</h2>
                  </div>
                  <div class="panel goal-panel">
                    <h1 class="bg-unit">Y</h1>
                    <h2>当前教室：{{ classroomId.value }}</h2>
                  </div>
                </div>
                <div class="timer">
                  <h2>学习进度</h2>
                  <el-progress type="circle" :percentage="studyProgress" :width="200" :stroke-width="10">
                    <template #default="{ percentage }">
                      <span class="progress-value">{{ percentage.toFixed(0) }}%</span>
                      <span class="progress-label">已完成</span>
                    </template>
                  </el-progress>
                </div>
                <el-button type="danger" @click="stopStudying">退出</el-button>
              </div>
              <div class="right-panel">
                <div class="container">
                  <div id="slide">
                    <div class="item" style="background-image: url(/image/studing.jpeg);">
                      <div class="content">
                        <div class="name">Effort</div>
                        <div class="description">"成功的关键在于坚持不懈，永不放弃。每一次努力，都是离目标更近一步的机会。"</div>
                        <button>Cheer up</button>
                      </div>
                    </div>
                    <div class="item" style="background-image: url(/image/studing.jpeg);">
                      <div class="content">
                        <div class="name">Effort</div>
                        <div class="description">"奋斗的青春最美丽。无论前方的路有多艰辛，只要你心中有梦想，就一定能到达彼岸。"</div>
                        <button>Cheer up</button>
                      </div>
                    </div>
                    <div class="item" style="background-image: url(/image/studing.jpeg);">
                      <div class="content">
                        <div class="name">Effort</div>
                        <div class="description">"考研、考公，都是改变一生的机会。相信自己，未来一定会因你的努力而闪耀。"</div>
                        <button>Cheer up</button>
                      </div>
                    </div>
                    <div class="item" style="background-image: url(/image/studing.jpeg);">
                      <div class="content">
                        <div class="name">Effort</div>
                        <div class="description">"每一次付出，都会在未来的某一刻得到回报。坚持就是胜利，努力定能成就梦想！"</div>
                        <button>Cheer up</button>
                      </div>
                    </div>
                  </div>
                  <div class="buttons">
                    <button id="previous">
                      <i class="fa-solid fa-angle-left"></i>
                    </button>
                    <button id="next">
                      <i class="fa-solid fa-angle-right"></i>
                    </button>
                  </div>
                </div>
              </div>
            </div>
            <div v-else class="loading">
              <LoadingEffect />
            </div>
          </el-main>
        </el-container>
      </el-container>
    </div>
  </template>
  
  <style scoped>
  .common-layout {
    height: 100vh;
    display: flex;
    flex-direction: column;
  }
  
  .header {
    background-color: #409EFF;
    color: white;
    text-align: center;
    font-size: 24px;
    line-height: 60px;
    flex-shrink: 0;
  }
  
  .el-container {
    flex-grow: 1;
    overflow: hidden;
  }
  
  .aside {
    background-color: #f0f2f5;
    padding: 20px;
    flex-shrink: 0;
  }
  
  .el-main {
    padding: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  
  .study-session {
    flex-grow: 1;
    display: flex;
    flex-direction: row;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    position: relative;
    justify-content: space-between;
    padding: 20px;
    border-radius: 15px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    overflow-y: auto;
  }
  
  .left-panel {
    flex: 1 1 40%;
    background: rgba(255, 255, 255, 0.9);
    padding: 30px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    border-radius: 15px;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
  }
  
  .right-panel {
    flex: 1 1 90%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.9);
    padding: 0;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    border-radius: 15px;
    position: relative;
    overflow: hidden;
  }
  
  .timer {
    margin-top: 30px;
    text-align: center;
  }
  
  .timer h2 {
    margin-bottom: 20px;
    color: #333;
  }
  
  .loading {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  .title {
    font-size: 2em;
    margin-bottom: 1em;
    color: #222;
    text-align: center;
  }
  
  .units {
    display: flex;
    flex-direction: column;
    gap: 1.5em;
  }
  
  .panel {
    background: #f0f2f5;
    border-radius: 15px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    padding: 1.5em;
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
  }
  
  .panel:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  }
  
  .panel:nth-child(1) { background: #ff6b6b; }
  .panel:nth-child(2) { background: #4ecdc4; }
  .panel:nth-child(3) { background: #45b7d1; }
  .panel:nth-child(4) { background: #f7b731; }
  .panel:nth-child(5) { background: #aa42ea; }
  
  .bg-unit {
    margin: 0;
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    font-size: 5em;
    z-index: 0;
    pointer-events: none;
    opacity: 0.2;
    color: rgba(255, 255, 255, 0.5);
  }
  
  .panel h2 {
    position: relative;
    z-index: 1;
    color: white;
    font-size: 1.2em;
    margin-bottom: 0.5em;
  }
  
  .progress-value {
    display: block;
    font-size: 28px;
    font-weight: bold;
    color: #409EFF;
  }
  
  .progress-label {
    display: block;
    font-size: 14px;
    margin-top: 5px;
    color: #606266;
  }
  
  .el-button {
    margin-top: 20px;
  }
  
  .container {
    position: relative;
    width: 100%;
    height: 100%;
    padding: 0;
    box-shadow: none;
  }
  
  #slide {
    width: max-content;
    margin-top: 20px;
  }
  
  .item {
    width: 150px;
    height: 150px;
    background-position: 50% 50%;
    display: inline-block;
    transition: 0.5s;
    background-size: cover;
    position: absolute;
    z-index: 1;
    top: 90%;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    box-shadow: 0 30px 50px #505050;
  }
  
  .item:nth-child(1),
  .item:nth-child(2) {
    left: 0;
    top: 0;
    transform: translate(0, 0);
    border-radius: 0;
    width: 100%;
    height: 100%;
    box-shadow: none;
  }
  
  .item:nth-child(3) {
    left: 75%;
  }
  
  .item:nth-child(4) {
    left: calc(75% + 200px);
  }
  
  .item .content {
    position: absolute;
    top: 50%;
    left: 100px;
    width: 300px;
    text-align: left;
    padding: 0;
    color: #eee;
    transform: translate(0, -50%);
    display: none;
    font-family: system-ui;
  }
  
  .item:nth-child(2) .content {
    display: block;
    z-index: 111111;
  }
  
  .item .description {
    margin: 20px 0;
    font-weight: bold;
    opacity: 0;
    animation: showcontent 1s ease-in-out 0.3s 1 forwards;
  }
  
  .item .name {
    font-size: 40px;
    font-weight: bold;
    opacity: 0;
    animation: showcontent 1s ease-in-out 1 forwards;
  }
  
  .item button {
    padding: 10px 20px;
    border: none;
    opacity: 0;
    animation: showcontent 1s ease-in-out 0.0s 1 forwards;
  }
  
  @keyframes showcontent {
    from {
      opacity: 0;
      transform: translate(0, 100px);
      filter: blur(33px);
    }
    to {
      opacity: 1;
      transform: translate(0, 0);
      filter: blur(0);
    }
  }
  
  .buttons {
    position: absolute;
    bottom: 30px;
    z-index: 222222;
    text-align: center;
    width: 100%;
  }
  
  .buttons button {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: 1px solid #555;
    transform: 0.5s;
  }
  
  .buttons button:hover {
    background-color: #bac383;
  }
  
  @media (max-width: 768px) {
    .study-session {
      flex-direction: column;
    }
  
    .left-panel, .right-panel {
      width: 100%;
      margin-bottom: 20px;
    }
  
    .left-panel {
      margin-bottom: 0;
    }
  }
  </style>
  
  <script setup>
  import { ref, onMounted, onUnmounted, computed, nextTick } from 'vue'
  import { useRoute, useRouter } from 'vue-router'
  import { ElProgress } from 'element-plus'
  import LoadingEffect from '/src/components/LoadingEffect.vue'
  
  
  const route = useRoute()
  const router = useRouter()
  
  const goal = ref(route.query.goal)
  const studyDuration = ref(parseInt(route.query.studyDuration) * 60)
  const restDuration = ref(parseInt(route.query.restDuration) * 60)
  
  const studyTime = ref(studyDuration.value)
  const restTime = ref(restDuration.value)
  const classroomId = computed(() => route.params.id)
  const classroom = computed(() => store.getters.getClassroomById(classroomId.value))
  console.log(classroomId.value)
  
  let studyInterval
  
  const studyPlan = ref(null)
  
  const studyProgress = computed(() => {
    return ((studyDuration.value - studyTime.value) / studyDuration.value) * 100
  })
  
  onMounted(async () => {
    await fetchStudyPlan().then(plan => {
      studyPlan.value = plan
    })
    startStudyTimer()
  
    nextTick(() => {
      document.getElementById('next').onclick = function() {
        let lists = document.querySelectorAll('.item');
        document.getElementById('slide').appendChild(lists[0]);
      }
      document.getElementById('previous').onclick = function() {
        let lists = document.querySelectorAll('.item');
        document.getElementById('slide').insertBefore(lists[lists.length - 1], lists[0]);
      }
    })
  })
  
  onUnmounted(() => {
    clearInterval(studyInterval)
  })
  
  function fetchStudyPlan() {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          classroomId: '12345',
          goal: '通过考试',
          currentClassroom: '101'
        })
      }, 1000)
    })
  }
  
  function startStudyTimer() {
    studyInterval = setInterval(() => {
      if (studyTime.value > 0) {
        studyTime.value--
      } else {
        clearInterval(studyInterval)
        router.push({ name: 'classroomDetail', params: { id: route.params.id } })
      }
    }, 1000)
  }
  
  function stopStudying() {
    clearInterval(studyInterval)
    studyTime.value = studyDuration.value
    restTime.value = restDuration.value
    router.push({ name: 'classroomDetail', params: { id: route.params.id } })
  }
  
  function formatTime(seconds) {
    const minutes = Math.floor(seconds / 60)
    const secs = seconds % 60
    return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
  }
  </script>